<?php
/*
 * Summary : Created By Avik on 11th April,2010
 * Version : 1.0
 * Contents: Class to fetch All Data
 * Chnage on 16th April: Category to Theme.
 */

//#########INCLUDE FILES########

include_once 'conf/db-config.php';
include_once 'conf/error.php';

//##############################


	class clsFetchData
	{
		/*
		 * Fetch Data method fetches all the data depending on is_approved input and return as an array
		 */
		public function FetchDataToArray($is_approved)
		{
			$arrDataBind = array();
			$count = 0;
			if($is_approved!="ALL")
			{
				$selectQuery ="SELECT * FROM tbl_globaltodo_listings WHERE is_approved='$is_approved'";
			}
			else
			{
				$selectQuery ="SELECT * FROM tbl_globaltodo_listings";
			}	
			$run = mysql_query($selectQuery);
			while ($data = mysql_fetch_array($run))
			{
				$arrDataBind[$count]['id'] = $data['unique_id'];
				$arrDataBind[$count]['sl_no'] = stripslashes($data['id']);
				$arrDataBind[$count]['city'] = stripslashes($data['city']);
				$arrDataBind[$count]['theme'] = stripslashes($data['category']);
				$arrDataBind[$count]['business_title'] = stripslashes($data['business_title']);
				$arrDataBind[$count]['business_desc'] = str_replace("&","&amp;",stripslashes($data['business_desc']));
				$arrDataBind[$count]['business_summary'] = stripslashes($data['business_summary']);
				$arrDataBind[$count]['phone_number'] = stripslashes($data['phone_number']);
				$arrDataBind[$count]['street_address'] = stripslashes($data['street_address']);
				$arrDataBind[$count]['email_address'] = $data['email_address'];
				$arrDataBind[$count]['website_address'] = $data['website_address'];
				$arrDataBind[$count]['audio_url'] = $data['audio_url'];
				$arrDataBind[$count]['video_url'] = $data['video_url'];
				$arrDataBind[$count]['sales_rep'] = stripslashes($data['sales_rep']);
				$arrDataBind[$count]['state'] = stripslashes($data['state']);
				$arrDataBind[$count]['zipcode'] = $data['zipcode'];
				$arrDataBind[$count]['business_logo'] = $data['business_logo'];
				$arrDataBind[$count]['image_1'] = $data['image_1'];
				$arrDataBind[$count]['image_2'] = $data['image_2'];
				$arrDataBind[$count]['image_3'] = $data['image_3'];
				$arrDataBind[$count]['image_4'] = $data['image_4'];
				$arrDataBind[$count]['is_approved'] = $data['is_approved'];
				$arrDataBind[$count]['tollfree_number'] = $data['tollfree_number'];
				$arrDataBind[$count]['fax_number'] = $data['fax_number'];
				$arrDataBind[$count]['barcode_image'] = $data['barcode_image'];
				$count ++;
			}
			return $arrDataBind;
		}
		
		/*
		 * FETCH ALL CATEGORIES FROM DATABASE
		 */
		
		public function FetchAllCategories()
		{
			$arrDataBind = array();
			$count = 0;
			
			$sql = "SELECT `id`, `name` FROM tbl_category_listing ORDER BY `name`";
			$run = mysql_query($sql);
			while ($data = mysql_fetch_array($run))
			{
				$arrDataBind[$count]['sl_no'] = $data['id'];
				$arrDataBind[$count]['name'] = stripslashes($data['name']);
				$count ++;
			}
			
			return $arrDataBind;
		}
		
		public function getAllDataForId($id)
		{
			$arrDataBind = array();
			$selectQuery ="SELECT * FROM tbl_globaltodo_listings WHERE id=".$id;
			//file_put_contents("C:\\testD.txt",$selectQuery);
			$run = mysql_query($selectQuery);
			while ($data = mysql_fetch_array($run))
			{
				$arrDataBind['id'] = $data['unique_id'];
				$arrDataBind['sl_no'] = stripslashes($data['id']);
				$arrDataBind['city'] = stripslashes($data['city']);
				$arrDataBind['theme'] = stripslashes($data['category']);
				$arrDataBind['business_title'] = stripslashes($data['business_title']);
				$arrDataBind['business_desc'] = stripslashes($data['business_desc']);
				$arrDataBind['business_summary'] = stripslashes($data['business_summary']);
				$arrDataBind['phone_number'] = stripslashes($data['phone_number']);
				$arrDataBind['street_address'] = stripslashes($data['street_address']);
				$arrDataBind['email_address'] = $data['email_address'];
				$arrDataBind['website_address'] = $data['website_address'];
				$arrDataBind['audio_url'] = $data['audio_url'];
				$arrDataBind['video_url'] = $data['video_url'];
				$arrDataBind['sales_rep'] = stripslashes($data['sales_rep']);
				$arrDataBind['state'] = stripslashes($data['state']);
				$arrDataBind['zipcode'] = $data['zipcode'];
				$arrDataBind['business_logo'] = $data['business_logo'];
				$arrDataBind['image_1'] = $data['image_1'];
				$arrDataBind['image_2'] = $data['image_2'];
				$arrDataBind['image_3'] = $data['image_3'];
				$arrDataBind['image_4'] = $data['image_4'];
				$arrDataBind['is_approved'] = $data['is_approved'];
				$arrDataBind['tollfree_number'] = $data['tollfree_number'];
				$arrDataBind['fax_number'] = $data['fax_number'];
				$arrDataBind['barcode_image'] = $data['barcode_image'];
			}
			return $arrDataBind;
		}
		
		/*
		 * Following Function is used for showing approved data of certain categories.
		 */
		
		public function FetchDataToArrayWithCategories($is_approved, $arrCity)
		{
			$arrDataBind = array();
			if(count($arrCity)>0)
			{
				$strImplodedCategory = implode("','", $arrCity);
			}
			//print $strImplodedCategory;
			$count = 0;
			
			$selectQuery ="SELECT * FROM tbl_globaltodo_listings WHERE city IN ('$strImplodedCategory') AND is_approved='$is_approved'";
				
			//print $selectQuery;
			$run = mysql_query($selectQuery);
			while ($data = mysql_fetch_array($run))
			{
				$arrDataBind[$count]['id'] = $data['unique_id'];
				$arrDataBind[$count]['sl_no'] = stripslashes($data['id']);
				$arrDataBind[$count]['city'] = stripslashes($data['city']);
				$arrDataBind[$count]['theme'] = stripslashes($data['category']);
				$arrDataBind[$count]['business_title'] = stripslashes($data['business_title']);
				$arrDataBind[$count]['business_desc'] = str_replace("&","&amp;",stripslashes($data['business_desc']));
				$arrDataBind[$count]['business_summary'] = stripslashes($data['business_summary']);
				$arrDataBind[$count]['phone_number'] = stripslashes($data['phone_number']);
				$arrDataBind[$count]['street_address'] = stripslashes($data['street_address']);
				$arrDataBind[$count]['email_address'] = $data['email_address'];
				$arrDataBind[$count]['website_address'] = $data['website_address'];
				$arrDataBind[$count]['audio_url'] = $data['audio_url'];
				$arrDataBind[$count]['video_url'] = $data['video_url'];
				$arrDataBind[$count]['sales_rep'] = stripslashes($data['sales_rep']);
				$arrDataBind[$count]['state'] = stripslashes($data['state']);
				$arrDataBind[$count]['zipcode'] = $data['zipcode'];
				$arrDataBind[$count]['business_logo'] = $data['business_logo'];
				$arrDataBind[$count]['image_1'] = $data['image_1'];
				$arrDataBind[$count]['image_2'] = $data['image_2'];
				$arrDataBind[$count]['image_3'] = $data['image_3'];
				$arrDataBind[$count]['image_4'] = $data['image_4'];
				$arrDataBind[$count]['is_approved'] = $data['is_approved'];
				$arrDataBind[$count]['tollfree_number'] = $data['tollfree_number'];
				$arrDataBind[$count]['fax_number'] = $data['fax_number'];
				$arrDataBind[$count]['barcode_image'] = $data['barcode_image'];
				$count ++;
			}
			return $arrDataBind;
		}
		
		
		/*
		 * Fetch Category FOr User
		 */
		
		public function fetchCityForUser($uid)
		{
			$arrCity = array();
			$sqlCity = "SELECT distinct(city) FROM tbl_user_city WHERE user_id=$uid";
			
			$run = mysql_query($sqlCity);
			$count=0;
			while ($data = mysql_fetch_array($run))
			{
				$arrCity[$count] = $data['city'];
				$count++;
			}
			
			return $arrCity;
		}
		
		public function fetchAllCity()
		{
			$arrCity = array();
			$sqlCity = "SELECT distinct(city_name) as city FROM tbl_city_names ";
			$run = mysql_query($sqlCity);
			$count=0;
			while ($data = mysql_fetch_array($run))
			{
				$arrCity[$count] = $data['city'];
				$count++;
			}
			
			return $arrCity;
		}
		
		/*
		 * User Lists With Their city name
		 */
		
		public function getAllUsersCity()
		{
			$arrUserCity = array();
			$sql ="SELECT id,user_name FROM tbl_users WHERE upper(is_admin)='N'";	
			$run = mysql_query($sql);
			$counter=0;
			while ($data = mysql_fetch_array($run))
			{
				$userId = $data['id'];
				$userName = $data['user_name'];
				$sqlCity = "SELECT city FROM tbl_user_city WHERE user_id=$userId";
				$runCity = mysql_query($sqlCity);
				$count=0;
				unset($arrCity);
				while ($dtaCt = mysql_fetch_array($runCity))
				{
					 $arrCity[$count]= $dtaCt['city'];
					 $count++;
				}
				$strCity = implode(",",$arrCity);
				$arrUserCity[$counter]["user_name"] = $userName;
				$arrUserCity[$counter]["user_id"] = $userId;
				$arrUserCity[$counter]["city"] = $strCity;
				$counter++;
			}
			return $arrUserCity;
		}
		
		
		public function getAllUsers()
		{
			$users= array();
			$sqlSelect = "SELECT user_name from tbl_users";
			$run = mysql_query($sqlSelect);
			while ($data = mysql_fetch_array($run))
			{
				array_push($users,$data['user_name']);
			}
			return $users;
		}
		
		
	}  






?>